/**
 * Project Name:Android_Car_Example
 * File Name:RegeocodeTask.java
 * Package Name:com.amap.api.car.example
 * Date:2015年4月2日下午6:24:53
 */

package com.yxhl.zoume.core.func.map;

import android.content.Context;

import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.GeocodeSearch.OnGeocodeSearchListener;
import com.amap.api.services.geocoder.RegeocodeQuery;
import com.amap.api.services.geocoder.RegeocodeResult;
import com.yxhl.zoume.core.func.map.info.PoiInfo;
import com.yxhl.zoume.utils.LOG;

import java.util.ArrayList;

/**
 * 用于逆地理编码
 * Modified by Xboy on 15/12/4.
 */
public class RegeocodeTask implements OnGeocodeSearchListener {
    private static final float SEARCH_RADIUS = 50;
    private OnLocationGetListener mOnLocationGetListener;

    private GeocodeSearch mGeocodeSearch;

    public RegeocodeTask(Context context) {
        mGeocodeSearch = new GeocodeSearch(context);
        mGeocodeSearch.setOnGeocodeSearchListener(this);
    }

    public void regecodeSearchByLatlng(double latitude, double longitude) {
        RegeocodeQuery regecodeQuery = new RegeocodeQuery(new LatLonPoint(
                latitude, longitude), SEARCH_RADIUS, GeocodeSearch.AMAP);
        mGeocodeSearch.getFromLocationAsyn(regecodeQuery);
    }

    public void setOnLocationGetListener(
            OnLocationGetListener onLocationGetListener) {
        mOnLocationGetListener = onLocationGetListener;
    }

    @Override
    public void onGeocodeSearched(GeocodeResult arg0, int arg1) {

    }

    @Override
    public void onRegeocodeSearched(RegeocodeResult regeocodeReult,
                                    int resultCode) {
        if (resultCode == 0) {
            if (regeocodeReult != null
                    && regeocodeReult.getRegeocodeAddress() != null
                    && mOnLocationGetListener != null) {

                ArrayList<PoiItem> items = (ArrayList<PoiItem>) regeocodeReult.getRegeocodeAddress().getPois();
                ArrayList<PoiInfo> pois = new ArrayList<>();
                for (PoiItem item : items) {

                    LOG.v("Xboy", "getAdName = " + item.getAdName() + "getTitle = " + item.getTitle() + "getLatLonPoint = " + item.getPoiId() + item.getLatLonPoint().toString() + "lat = " + item.getLatLonPoint().getLatitude() + "lng = " + item.getLatLonPoint().getLongitude());

                    PoiInfo poi = new PoiInfo(item.getLatLonPoint().getLatitude(), item.getLatLonPoint().getLongitude(), item.getTitle(),
                            item.getSnippet());

                    poi.setCity(item.getCityName());
                    poi.setaCode(regeocodeReult.getRegeocodeAddress().getAdCode());
                    pois.add(poi);
                }

                if (null != mOnLocationGetListener) {
                    mOnLocationGetListener.onRegecodeGet(pois);
                }
            }
        }
        //TODO 可以根据app自身需求对查询错误情况进行相应的提示或者逻辑处理
    }
}
